package com.xsoft.sms.cmpp20.config;

import com.xsoft.sms.utils.Helpers;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


/**
 * package: {PACKAGE_NAME}
 * User: zhanglm
 * Date: 12-9-18
 * Time: 上午12:14
 * Description:
 * 目录结构
 *
 *  root->
 *      bin-> 运行脚本
 *      lib-> 所有JAR包
 *      conf-> 配置文件
 */
public class CMPP20Config {


	private static Logger logger = Logger.getLogger(CMPP20Config.class);

	/**
	 * <cmpp20>
	 <!--网关地址-->
	 <gwaddr>211.139.32.5</gwaddr>
	 <!--网关端口-->
	 <gwport>7890</gwport>
	 <!--接入账号-->
	 <account>622734</account>
	 <!--接入密码-->
	 <password>m2m123</password>
	 <!--接入码-->
	 <acccode>106571734</acccode>
	 <!--spid-->
	 <spid>622734</spid>
	 </cmpp20>
	 */

	static {
		String runtimePath  = Helpers.getRuntimePath();
		String cmpp20ConfigPath = runtimePath + "../conf/cmpp20-gateway-config.xml";
		try {

			Document document       = new SAXReader().read(cmpp20ConfigPath);
			Element rootElement     = document.getRootElement();
			Element addrElement     = rootElement.element("gwaddr");
			Element portElement     = rootElement.element("gwport");
			Element accountElement  = rootElement.element("account");
			Element passwordElement = rootElement.element("password");
			Element acccodeElement  = rootElement.element("acccode");
			Element spidElement     = rootElement.element("spid");
			Element rtcElement      = rootElement.element("retryCount");
			Element rtiElement      = rootElement.element("retryInterval");
			Element rscElement      = rootElement.element("resendCount");
			Element rsiElement      = rootElement.element("resendInterval");
			Element wsElement       = rootElement.element("windowSize");

			gwAddr          = addrElement.getText();
			gwPort          = Integer.valueOf(portElement.getText());
			account         = accountElement.getText();
			password        = passwordElement.getText();
			accCode         = acccodeElement.getText();
			spId            = spidElement.getText();
			retryCount      = Integer.valueOf(rtcElement.getText());
			retryInterval   = Integer.valueOf(rtiElement.getText());
			resendCount     = Integer.valueOf(rscElement.getText());
			resendInterval  = Integer.valueOf(rsiElement.getText());
			windowSize      = Integer.valueOf(wsElement.getText());
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.fatal(ex.getMessage());
			System.exit(0);
		}
		logger.info(CMPP20Config.getAccCode() + "\n" +
				CMPP20Config.getAccount() + "\n" +
				CMPP20Config.getGwAddr() + "\n" +
				CMPP20Config.getPassword() + "\n" +
				CMPP20Config.getSpId() + "\n" +
				CMPP20Config.getGwPort() + "\n" +
				CMPP20Config.getRetryCount() + "\n" +
				CMPP20Config.getRetryInterval() + "\n" +
				CMPP20Config.getResendCount() + "\n" +
				CMPP20Config.getResendInterval() + "\n" +
				CMPP20Config.getWindowSize() + "\n");

	}

	public static String getGwAddr() {
		return gwAddr;
	}

	public static int getGwPort() {
		return gwPort;
	}

	public static String getAccount() {
		return account;
	}

	public static String getPassword() {
		return password;
	}

	public static String getAccCode() {
		return accCode;
	}

	public static String getSpId() {
		return spId;
	}

	public static int getRetryCount() {
		return retryCount;
	}

	public static int getRetryInterval() {
		return retryInterval;
	}

	public static int getResendCount() {
		return resendCount;
	}

	public static int getResendInterval() {
		return resendInterval;
	}

	public static int getWindowSize() {
		return windowSize;
	}

	public static String   gwAddr;
	public static int      gwPort;
	public static String   account;
	public static String   password;
	public static String   accCode;
	public static String   spId;
	public static int      retryCount;
	public static int      retryInterval;
	public static int      resendCount;
	public static int      resendInterval;
	public static int      windowSize;

	public static void main(String[] args) {
//		System.out.println(CMPP20Config.getAccCode() + "\n" +
//				CMPP20Config.getAccount() + "\n" +
//				CMPP20Config.getGwAddr() + "\n" +
//				CMPP20Config.getPassword() + "\n" +
//				CMPP20Config.getSpId() + "\n" +
//				CMPP20Config.getGwPort() + "\n");
	}

}
